package com.laicunba.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {
	public static final String EMPTY_STR = "";
	public static final String SYMBOL_COMMA = ",";

	public static boolean isNotEmpty(String str) {
		return !isEmpty(str);
	}

	public static boolean isNotEmpty(String... str) {
		for (String s : str) {
			if (s == null || s.trim().equals("")) {
				return false;
			}
		}
		return true;
	}

	public static boolean isEmpty(String str) {
		// TODO Auto-generated method stub
		return str == null || str.trim().equals("");
	}

	public static boolean isEmpty(String... str) {
		for (String s : str) {
			if (s == null || s.trim().equals("")) {
				return true;
			}
		}
		return false;
	}

	public static String genGUID() {

		return java.util.UUID.randomUUID().toString();

	}

	public static List<String> doScattered(String str, int len) {
		List<String> list = new ArrayList();
		while (true) {
			if (str.length() > len) {
				list.add(str.substring(0, len));
				str = str.substring(len);
			} else {
				list.add(str);
				break;
			}
		}

		return list;
	}

	public static String genHeadPicDiv(String str) {
		List<String> list = doScattered(str, 3);
		String path = "/";
		for (String tmp : list) {
			path += tmp + "/";
		}
		return path;
	}

	private static final String MOBILE_REGEX = "(1[0-9]{10})|([\\+]?[0-9]{11,15})";

	/**
	 * 手机号码?
	 * 
	 * @param mobile
	 * @return
	 */
	public static boolean isMobileNumber(String mobile) {
		if (isEmpty(mobile)) {
			return false;
		}
		return mobile.matches(MOBILE_REGEX);
	}

	private static Random random = new Random();

	/**
	 * 随机的数字字符串
	 * 
	 * @param len
	 * @return
	 */
	public static String randomNumber(int len) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < len; i++) {
			sb.append(random.nextInt(10));
		}
		return sb.toString();
	}

	public static String format(String tmp, Object... value) {
		return String.format(tmp, value);
	}

	/**
	 * sha1加密
	 * 
	 * @param str
	 * @return
	 */
	public static String sha1(String str) {
		try {
			MessageDigest digest = java.security.MessageDigest.getInstance("SHA-1");
			digest.update(str.getBytes());
			byte messageDigest[] = digest.digest();
			// Create Hex String
			StringBuffer hexString = new StringBuffer();
			// 字节数组转换为 十六进制 数
			for (int i = 0; i < messageDigest.length; i++) {
				String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
				if (shaHex.length() < 2) {
					hexString.append(0);
				}
				hexString.append(shaHex);
			}
			return hexString.toString();

		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return "";
	}

	public static String sha(String decript) {
		try {
			MessageDigest digest = java.security.MessageDigest.getInstance("SHA");
			digest.update(decript.getBytes());
			byte messageDigest[] = digest.digest();
			// Create Hex String
			StringBuffer hexString = new StringBuffer();
			// 字节数组转换为 十六进制 数
			for (int i = 0; i < messageDigest.length; i++) {
				String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
				if (shaHex.length() < 2) {
					hexString.append(0);
				}
				hexString.append(shaHex);
			}
			return hexString.toString();

		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return "";
	}

	public static String md5(String input) {
		try {
			// 获得MD5摘要算法的 MessageDigest 对象
			MessageDigest mdInst = MessageDigest.getInstance("MD5");
			// 使用指定的字节更新摘要
			mdInst.update(input.getBytes());
			// 获得密文
			byte[] md = mdInst.digest();
			// 把密文转换成十六进制的字符串形式
			StringBuffer hexString = new StringBuffer();
			// 字节数组转换为 十六进制 数
			for (int i = 0; i < md.length; i++) {
				String shaHex = Integer.toHexString(md[i] & 0xFF);
				if (shaHex.length() < 2) {
					hexString.append(0);
				}
				hexString.append(shaHex);
			}
			return hexString.toString();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return "";
	}

	private static final String RANDOM_CHARS = "1234pqrstuv590abcdefghijklmnowxyzAB678CDEFGHIJK1234567890LMNOPQRSTUVWXYZ";

	public static String random(int len) {
		int charSize = RANDOM_CHARS.length();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < len; i++) {
			sb.append(random.nextInt(charSize));
		}
		return sb.toString();
	}

	private static final String NUMBER_REGEX = "^[0-9]+$";

	public static boolean isNumber(String str) {
		return isNotEmpty(str) && str.matches(NUMBER_REGEX);
	}

	public static String endSubstring(String str, int len) {
		if (isEmpty(str)) {
			return "";
		}
		if (str.length() <= len) {
			return str;
		}

		return str.substring(str.length() - len);
	}

	public static boolean hasEmpty(String... str) {
		for (String s : str) {
			if (s == null || s.trim().equals("")) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 字符串转成数字,如果为空则返回0
	 * 
	 * @param str
	 * @return
	 */
	public static int toInt(String str) {
		if (!isNumber(str)) {
			return 0;
		}
		return Integer.parseInt(str);
	}

	public static String concatComma(String... strs) {
		if (strs == null) {
			return EMPTY_STR;
		}
		StringBuilder sb = new StringBuilder();
		for (String str : strs) {
			sb.append(str).append(SYMBOL_COMMA);
		}
		return sb.toString();
	}
	/**
	 * 试试运行
	 * @param luckStr
	 * @return
	 */
	public static char tryLuck(String luckStr){
		return luckStr.charAt(new Random().nextInt(luckStr.length()));
	}

	public static String findOne(String orgStr,String regex){
		Matcher  matcher  = Pattern.compile(regex).matcher(orgStr);
		return matcher.find()?matcher.group():null;
	}	
	public static List<String> find(String orgStr,String regex){
		Matcher  matcher  = Pattern.compile(regex).matcher(orgStr);
		List<String> rs= new ArrayList();
		while(matcher.find()){
			rs.add(matcher.group());
		}
		return rs;
	}
}
